package com.yiren.dbaa.module.practice_set.map.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.module.practice_set.map.bean.LineBindBean;
import com.yiren.dbaa.module.practice_set.map.contract.MapReleaseContract;
import com.yiren.dbaa.module.practice_set.map.presenter.MapReleasePresenter;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/29 17:01
 * @description: 地图发布
 */
public class MapReleaseActivity extends BaseActivity implements MapReleaseContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_map_name)
    TextView tvMapName;
    @BindView(R.id.tv_station_name)
    TextView tvStationName;
    @BindView(R.id.tv_map_type)
    TextView tvMapType;
    @BindView(R.id.ll_update_detail)
    LinearLayout llUpdateDetail;
    @BindView(R.id.tv_update_detail)
    TextView tvUpdateDetail;
    @BindView(R.id.ll_update_detail_et)
    LinearLayout llUpdateDetailEt;
    @BindView(R.id.et_update_detail)
    EditText etUpdateDetail;
    @BindView(R.id.sp_update_way)
    Spinner spUpdateWay;
    @BindView(R.id.ll_update_way)
    LinearLayout llUpdateWay;
    @BindView(R.id.sp_update_user)
    Spinner spUpdateUser;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;
    @BindView(R.id.ll_update_user)
    LinearLayout llUpdateUser;
    @BindView(R.id.ll_map)
    LinearLayout llMap;

    private Intent intent;
    private MapEntity map;
    private List<LineBindBean> lineBindBeans;

    private int updateMode = 1; //更新方式 1:提示更新 2:强制更新

    private List<String> updateModes = new ArrayList<>();//更新方式
    private List<String> updateUsers = new ArrayList<>();//更新用户
    private MapReleaseContract.Presenter mapReleasePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_map);
        ButterKnife.bind(this);
        initData();
        setData();
    }

    private void initData() {
        tvTitle.setText("地图数据发布");
        intent = getIntent();
        map = (MapEntity) intent.getSerializableExtra("map");
        lineBindBeans = (List<LineBindBean>) intent.getSerializableExtra("lines");
        mapReleasePresenter = new MapReleasePresenter(this);
    }

    private void setData() {
        if (map != null) {
            tvMapName.setText(map.getName());
            tvStationName.setText(map.getBaseName());
            if (map.getUseRange() == 1) {
                tvUsingRangeBt.setVisibility(View.VISIBLE);
            } else {
                tvUsingRangeBt.setVisibility(View.GONE);
            }
            if (map.getMapType() == 1) {
                tvMapType.setText("考试路段");
            } else {
                tvMapType.setText("训练路段");
            }
            if (map.getMapVer() == 0) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llMap.getLayoutParams();
                lp.topMargin = 80;
                llMap.setLayoutParams(lp);
                tvUpdateDetail.setText("首次发布");
                llUpdateUser.setVisibility(View.GONE);
                llUpdateWay.setVisibility(View.GONE);
                llUpdateDetail.setVisibility(View.VISIBLE);
                llUpdateDetailEt.setVisibility(View.GONE);
            } else {
                llUpdateUser.setVisibility(View.VISIBLE);
                llUpdateWay.setVisibility(View.VISIBLE);
                llUpdateDetail.setVisibility(View.GONE);
                llUpdateDetailEt.setVisibility(View.VISIBLE);
            }
        }
        //数据源
        updateModes.add("提示更新");
        updateModes.add("强制更新");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateModes);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateWay.setAdapter(adapter);
        spUpdateWay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                updateMode = position + 1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        updateUsers.add("个别用户");
        updateUsers.add("全部用户");
        adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateUsers);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateUser.setAdapter(adapter);
        if (map.getUseRange() == 2) {
            spUpdateUser.setSelection(1);
        } else {
            spUpdateUser.setSelection(0);
        }
        spUpdateUser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                map.setUseRange(position + 1);
                if (position == 0) {
                    tvUsingRangeBt.setVisibility(View.VISIBLE);
                } else {
                    tvUsingRangeBt.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_release_map})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back: //返回
                finish();
                break;
            case R.id.tv_release_map: //发布
                if (map.getMapVer() == 0) {
                    map.setUpdateDetails(tvUpdateDetail.getText().toString().trim());
                } else {
                    String updateDetail = etUpdateDetail.getText().toString().trim();
                    if (StringUtils.isEmpty(updateDetail)) {
                        MessageBox.show(this, "请输入更新详情");
                        return;
                    } else {
                        map.setUpdateDetails(updateDetail);
                    }
                }
                map.setUpdateModel(updateMode);
                mapReleasePresenter.releaseMapToSer(map, lineBindBeans, this);
                break;
        }
    }

    @Override
    public void releaseMapSuccess() {
        MessageBox.MessageAndVoiceModel(this, "发布成功!");
        finish();
        startActivity(new Intent(this, PracticeCarSetActivity.class));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

}